mod trident;
mod viewcube;

use std::{f32::consts::PI, sync::LazyLock};
const PI_2: f32 = PI / 2.0;
const PI_4: f32 = PI / 4.0;
const PI_4_3: f32 = PI / 4.0 * 3.0;

#[derive(Component)]
pub struct SmallView;

/// Add this to target pan_orbit_camera.
/// panic when not set any ViewcubeBinding
#[derive(Component)]
pub struct ViewcubeBinding;

#[derive(Default, Clone, Resource)]
pub struct ViewcubeFocusCenter(pub Vec3);

pub static ROOT_QUAT: LazyLock<Quat> = LazyLock::new(|| Quat::from_rotation_x(-PI / 2.0));

use bevy::{
    color::palettes::css::{RED, WHITE},
    prelude::*,
    render::{camera::ScalingMode, view::RenderLayers},
};

/// The function `create_small_view` creates a small 3D camera view with a directional light in Rust
/// using the Bevy game engine.
pub(crate) fn create_small_view(mut commands: bevy::ecs::system::Commands) {
    // commands.insert_resource(AmbientLight {
    //     color: WHITE.into(),
    //     brightness: 0.02,
    // });
    commands
        .spawn((
            Camera {
                order: 13,
                clear_color: ClearColorConfig::Custom(RED.into()),
                ..Default::default()
            },
            Camera3d {
                depth_load_op: bevy::core_pipeline::core_3d::Camera3dDepthLoadOp::Clear(0.),
                ..default()
            },
            // Transform::from_xyz(0.6, 0.6, 4.0).looking_at(Vec3::new(0.6, 0.6, 0.6), Vec3::Y),
            Transform::from_xyz(0.0, 0.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
            // bevy::prelude::Projection::Perspective(PerspectiveProjection::default()),
            bevy::prelude::Projection::Orthographic(OrthographicProjection {
                scale: 0.02,
                near: 0.0,
                far: 100.0,
                scaling_mode: ScalingMode::WindowSize,
                ..OrthographicProjection::default_2d()
            }),
            RenderLayers::layer(13),
            SmallView,
        ))
        .with_children(|builder| {
            builder.spawn((
                DirectionalLight {
                    ..Default::default()
                },
                RenderLayers::layer(13),
                SmallView,
            ));
        });
}

pub mod prelude {
    pub use crate::trident::{BevyTridentArrow, BevyTridentAxis, BevyTridentCone};
    pub use crate::viewcube::BevyViewCubePlugin;
    pub use crate::ViewcubeBinding;
    pub use crate::ViewcubeFocusCenter;
}
